<?php
  class glbpay_debit {
    /**
	 * representing the payment method
	 */
	var $code;
	/**
	 * the displayed name for this payment method
	 */
	var $title;
	/**
	 * a soft name for this payment method
	 */
	var $description;
	/**
	 * $enabled determines whether this module shows or not
	 */
	var $enabled;
	/**
	 * sort order of display
	 */
	var $sort_order;
	/**
	 * order status setting for completed orders
	 */
	var $order_status;

	/**
	 * constructor function
	 */
	function glbpay_debit()
	{
		global $order, $messageStack;
		$this->code="glbpay_debit";
		$this->title=MODULE_PAYMENT_GLBPAYDEBIT_TEXT_ADMIN_TITLE;
		$this->description=MODULE_PAYMENT_GLBPAYDEBIT_TEXT_DESCRIPTION;
		$this->sort_order=MODULE_PAYMENT_GLBPAYDEBIT_SORT_ORDER;
        $this->enabled = ((MODULE_PAYMENT_GLBPAYDEBIT_STATUS == 'True') ? true : false);
        if ((int)MODULE_PAYMENT_GLBPAYDEBIT_ORDER_STATUS_ID > 0)
        {
            $this->order_status = MODULE_PAYMENT_GLBPAYDEBIT_ORDER_STATUS_ID;
        }
        if(is_object($order))
        {
        	$this->update_status();
        }

        if(MODULE_PAYMENT_GLBPAYDEBIT_HANDLER == 'Live')
        {
        	$this->form_action_url='https://pgw.glbpay.com/fgpay/payment';
        }
        elseif(MODULE_PAYMENT_GLBPAYDEBIT_HANDLER == 'Test')
        {
        	$this->form_action_url='http://pgw.grapay.net:8007/fgpay/payment';
        }
	}

	/**
	 *calculate zone matches and flag settings to determine whether this module should display to customers or not
	 */
	function update_status()
	{
		global $db,$order;
		if(($this->enabled==true)&&((int)MODULE_PAYMENT_GLBPAYDEBIT_ZONE > 0))
		{
			$check_flag=false;
			$check_query=$db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_GLBPAYDEBIT_ZONE . "' and zone_country_id = '" . $order->billing['country']['id'] . "' order by zone_id");
			while(!$check_query->EOF)
			{
				if($check_query->fields['zone_id'] < 1)
				{
					$check_flag=true;
					break;
				}
				elseif($check_query->fields['zone_id'] == $order->billing['zone_id'])
				{
					$check_flag=true;
					break;
				}
				$check_query->MoveNext();
			}
			if($check_flag==false)
			{
				$this->enabled=false;
			}
		}
	}

	/**
	 * JS validation which does error-checking of data-entry if this module is selected for use
	 */
	function javascript_validation()
	{
      return false;
    }

	/**
	 * Displays payment method name on the Checkout Payment Page
	 */
	function selection()
	{
		return array(    'id' => $this->code,
		                 'module'=> MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CATALOG_LOGO,
		                 'icon' => MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CATALOG_LOGO
		             );
	}

	/**
	 * evaluates the Credit Card Type for acceptance and the validity of the Credit Card Number & Expiration Date
	 * glbpay module is not collecting info, it simply skips this step.
	 */
	function pre_confirmation_check()
	{
		return false;
	}

	/**
	 * Display Credit Card Information on the Checkout Confirmation Page
	 * Since none is collected for glbpay before forwarding to glbpay site, this is skipped
	 */
	function confirmation($flag = '')
	{
		global $db;
		if ($flag == 'ok') {
			if (isset ($_SESSION['glbpay_order_id'])) {
				$res = $db->Execute("select * from " . TABLE_ORDERS . " where orders_id  = '" . $_SESSION['glbpay_order_id'] . "'");
				if(isset($res->fields)){
					$res = $res->fields;
					if($res['orders_status']==MODULE_PAYMENT_GLBPAYDEBIT_ORDER_STATUS_ID){
						$this->delete_order($_SESSION['glbpay_order_id']);
					}
				}
			}
			$this->create_order();
		}
	}

	/**
	 * creat order before payment
	 */
	private function create_order()
	{
		global $order, $order_totals;
		$order->info['payment_method'] = 'Credit card online payment';
		$order->info['payment_module_code'] = $this->code;
		$order->info['order_status'] = MODULE_PAYMENT_GLBPAYDEBIT_PROCESSING_STATUS_ID;
		$_SESSION['glbpay_order_id'] = $order->create($order_totals, 2);
		$order->create_add_products($_SESSION['glbpay_order_id']);
	}


	private function delete_order($order_id)
	{
	}

	function process_button()
	{
		$this->confirmation("ok");
		$process_button_string=glbpay_button_debit($_SESSION['glbpay_order_id']);
		return $process_button_string;
	}

	function before_process()
	{
		return false;
	}
	 function after_process()
	 {
	 	return false;
	 }
	/**
	 * Used to display error message details
	 */
	function output_error()
	{
		return false;
	}
	/**
	 * Check to see whether module is installed
	 */
	function check()
	{
		global $db;
		if(!isset($this->_check))
		{
			$check_query=$db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_GLBPAYDEBIT_STATUS'");
			$this->_check=$check_query->RecordCount();
		}
		return $this->_check;
	}

	/**
	 * Install the payment module and its configuration settings
	 */
	function install()
	{
		global $db, $language, $module_type;
		if (!defined('MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_1_1'))
		{
			 include(DIR_FS_CATALOG_LANGUAGES . $_SESSION['language'] . '/modules/' . $module_type . '/' . $this->code . '.php');
		}
	    $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_1_1 . "', 'MODULE_PAYMENT_GLBPAYDEBIT_STATUS', 'True', '" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_1_2 . "', '9', '1', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
        $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_2_1 . "', 'MODULE_PAYMENT_GLBPAYDEBIT_MERCHANTID', '', '" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_2_2 . "', '9', '2', now())");
        $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_3_1 . "', 'MODULE_PAYMENT_GLBPAYDEBIT_SSLKEY', '', '" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_3_2 . "', '9', '3', now())");
        $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_9_1 . "', 'MODULE_PAYMENT_GLBPAYDEBIT_ORDER_PREFIX', '', '" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_9_2 . "', '9', '4', now())");
        $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_4_1 . "', 'MODULE_PAYMENT_GLBPAYDEBIT_ZONE', '0', '" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_4_2 . "', '9', '5', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
        $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_5_1 . "', 'MODULE_PAYMENT_GLBPAYDEBIT_PROCESSING_STATUS_ID', '1', '" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_5_2 . "', '9', '6', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");
        $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_6_1 . "', 'MODULE_PAYMENT_GLBPAYDEBIT_ORDER_STATUS_ID', '2', '" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_6_2 . "', '9', '7', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");
        $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_7_1 . "', 'MODULE_PAYMENT_GLBPAYDEBIT_SORT_ORDER', '0', '" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_7_2 . "', '9', '8', now())");
        $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_8_1 . "', 'MODULE_PAYMENT_GLBPAYDEBIT_HANDLER', 'Live', '" . MODULE_PAYMENT_GLBPAYDEBIT_TEXT_CONFIG_8_2 . "', '9', '9', 'zen_cfg_select_option(array(\'Live\', \'Test\'), ', now())");

        $db->Execute("CREATE TABLE IF NOT EXISTS glbpay_session (
                      unique_id int(11) NOT NULL auto_increment,
                      session_id text NOT NULL,
                      orders_id text NOT NULL,
                      session_info blob NOT NULL,
                      session_date datetime NOT NULL default '0001-01-01 00:00:00',
                      PRIMARY KEY  (unique_id),
                      KEY idx_session_id_zen ( session_id(36) )
                      ) CHARACTER SET utf8 COLLATE utf8_general_ci;");
	}
	/**
	 * Remove the module and all its settings
	 */
	function remove()
	{
		global $db;
		$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key LIKE  'MODULE_PAYMENT_GLBPAY%'");
	}
	/**
	 * Internal list of configuration keys used for configuration of the module
	 */
	function keys()
	{
		return array(
         'MODULE_PAYMENT_GLBPAYDEBIT_STATUS',
         'MODULE_PAYMENT_GLBPAYDEBIT_MERCHANTID',
         'MODULE_PAYMENT_GLBPAYDEBIT_SSLKEY',
         'MODULE_PAYMENT_GLBPAYDEBIT_ORDER_PREFIX',
         'MODULE_PAYMENT_GLBPAYDEBIT_PROCESSING_STATUS_ID',
         'MODULE_PAYMENT_GLBPAYDEBIT_ORDER_STATUS_ID',
         'MODULE_PAYMENT_GLBPAYDEBIT_ZONE',
         'MODULE_PAYMENT_GLBPAYDEBIT_SORT_ORDER',
         'MODULE_PAYMENT_GLBPAYDEBIT_HANDLER'
         );
	}

  }
?>